import {
    getModelForClass,
    modelOptions, plugin,
    pre,
    prop,
    Ref,
} from "@typegoose/typegoose"
import { User } from "./User"
import autopopulate from "mongoose-autopopulate"

@plugin(autopopulate)
@pre<Post>("save", function () {
    this.title = this.title.toUpperCase()
})
@modelOptions({ schemaOptions: { collection: "posts", timestamps: true } })
export class Post {
    @prop({ required: true })
    title!: string

    @prop()
    content?: string

    @prop({ type: () => [String] })
    tags?: string[]

    @prop({ autopopulate: true,  required: true, ref: () => User })
    author!: Ref<User>
}

export const PostModel = getModelForClass(Post);